/**
 * 用来获取系统相关配置数据
 * 如：部门、车间、工段、
 * 需求类型、事件类型、
 * 已有的IT系统、需求的处理人员
 * 资产类别、资产品牌
 * 资产故障类型、资产鉴定状态(维修)
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.get('/', function(req, res, next) {

    let type = _.trim(String(req.query.type));

    let tableName = "";
    let whereClause = "";
    let msgType = "";

    switch(type){
        case 'depart':
            tableName = "sys_depart";
            msgType = "部门";
            break;
        case 'workshop':
            tableName = "sys_workshop";
            msgType = "车间";
            break;
        case 'process':
            tableName = "sys_process";
            msgType = "工段";
            break;
        case 'demandType':
            tableName = "sys_demand_type";
            msgType = "需求类型";
            break;
        case 'eventType':
            tableName = "sys_event_type";
            msgType = "事件类型";
            break;
        case 'software':
            tableName = "sys_software_system";
            msgType = "系统";
            break;
        case 'demandHandler':
            tableName = "sys_user";
            whereClause = " where demandhandle = 1";
            msgType = "需求处理人员";
            break;
        case 'equipmentType':
            tableName = "sys_equipment_type";
            msgType = "设备类别";
            break;
        case 'brand':
            tableName = "sys_equipment_brand";
            msgType = "品牌";
            break;
        case 'hitchType':
            tableName = "sys_hitch_type";
            msgType = "故障类型";
            break;
        case 'maintainState':
            tableName = "sys_mantain_state";
            msgType = "设备维修状态类型";
            break;
    }
    let data = {
        isSuccess: false,
        data: null,
        msg: msgType + "数据获取失败！"
    };
    let sql = "SELECT * FROM " + tableName + whereClause;
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            if(rows.length > 0 ) {
                data.data = rows;
                data.isSuccess = true;
                data.msg =  msgType +  "数据获取成功！";
                return res.json(data);
            } else {
                return res.json(data);
            }
        }
    })
});

module.exports = router;